<?php

#    Document   : upload_model
#    Created on : May 12, 2011, 2:31:20 AM
#    Author     : ClarkT AKA FalleN
#    Company    : Amodity
#    Web        : http://www.amodity.com
#    Copyright  : 2011

class upload_model extends CI_Model {

    function do_upload() {
        $this->config->load('upload');
        $config = array(
            'upload_path' => $this->config->item('upload_path'),
            'allowed_types' => $this->config->item('allowed_types'),
            'max_height' => $this->config->item('max_height'),
            'max_width' => $this->config->item('max_width'),
            'max_size' => $this->config->item('max_size'),
            'max_filename' => $this->config->item('max_filename'),
            'encrypt_name' => $this->config->item('encrypt_name'),
            'remove_spaces' => $this->config->item('remove_spaces')
        );

        $this->load->library('upload', $config);


        if (!$this->upload->do_upload()) {
            $error = $this->session->set_flashdata(array('error' => $this->lang->line('error_upload_fail')));
        } else {
            $image_data = $this->upload->data();

            //check if resize feilds have been set
            if ($this->input->post('resize_height') && $this->input->post('resize_width')) {
                $config['upload_path'] = $image_data['file_path'];
                $config['source_image'] = $image_data['full_path'];
                $config['create_thumb'] = FALSE;
                $config['width'] = $this->input->post('resize_width');
                $config['height'] = $this->input->post('resize_height');
                $this->load->library('image_lib', $config);
                if (!$this->_resize_image()) {
                    die('could not resize');
                }
            }

            if ($this->session->userdata('username')) {
                $is_logged_in = TRUE;
            }
            $data = array('upload_data' => $this->upload->data());
            if (isset($is_logged_in)) {
                $image_info = array(
                    'date' => date('m-d-Y'),
                    'name' => $data['upload_data']['file_name'],
                    'extension' => $data['upload_data']['image_type'],
                    'author' => $this->session->userdata('username'),
                    'width' => $data['upload_data']['image_width'],
                    'height' => $data['upload_data']['image_height'],
                    'file_size' => $data['upload_data']['file_size'],
                    'ip_address' => $this->session->userdata('ip_address')
                );
            } else {
                $image_info = array(
                    'date' => date('m-d-Y'),
                    'name' => $data['upload_data']['file_name'],
                    'extension' => $data['upload_data']['image_type'],
                    'width' => $data['upload_data']['image_width'],
                    'height' => $data['upload_data']['image_height'],
                    'file_size' => $data['upload_data']['file_size']
                );
            }
            $this->db->insert('images', $image_info);
            return $this->upload->data();
        }
    }

    function _resize_image() {

        $this->config->load('upload');
        $config['upload_path'] = $this->config->item('upload_path');
        $config['source_image'] = $config['upload_path'] . $this->upload->data('file_name');
//        //TODO: make db entires for settings
//        $config['maintain_ratio'] = $this->input->post('ratio') ? TRUE : FALSE;
//        $config['create_thumb'] = '';
        $config['width'] = $this->input->post('resize_width');
        $config['height'] = $this->input->post('resize_height');
        $this->load->library('image_lib', $config);

        if (!$this->image_lib->resize()) {
            return $error = $this->image_lib->display_errors();
        }
        return true;
    }

}

?>
